<template>
  <div class="white-box">
    <div class="table-mb">
      <el-button type="primary" @click="handleAdd({min_area_id:min_area_id})" v-if="min_area_id">添加单元</el-button>
    </div>

<!--    <div class="task-one">-->
<!--      <div class="task-one__item">-->
<!--        <span class="task-one__label">负责人姓名：</span>-->
<!--        <el-input placeholder="请输入负责人姓名" v-model="searchParams.area_fzr_name" @keyup.enter.native="handleSearch"-->
<!--                  clearable></el-input>-->
<!--      </div>-->
<!--      <div class="task-one__btn clearfix">-->
<!--        <el-button type="primary" :loading="searchBtnLoading" :icon="Search" @click="handleSearch">搜索</el-button>-->
<!--      </div>-->
<!--    </div>-->

    <el-table class="table-mb" v-loading="loading" :data="list" :header-cell-style="{ background: '#F5F7FA' }"
              element-loading-background="rgba(255, 255, 255, 0.6)">
      <el-table-column prop="min_area_title" label="所属小区"></el-table-column>
      <el-table-column prop="dong_number" label="XX栋"></el-table-column>
      <el-table-column prop="begin_unit_num" label="开始单元"></el-table-column>
      <el-table-column prop="end_unit_num" label="结束单元"></el-table-column>
      <el-table-column prop="layer_num" label="总层数"></el-table-column>
      <el-table-column prop="house_number" label="每层户数"></el-table-column>
      <el-table-column prop="created_at" label="创建时间"></el-table-column>
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-button link type="primary" @click="handleEdit(row,{min_area_unit_id:row.min_area_unit_id,min_area_id:row.min_area_id})">编辑</el-button>
          <el-popconfirm :persistent="false" title="确定删除吗？" confirm-button-text="确定" cancel-button-text="取消"
                         @confirm="handleDelete(row)">
            <template #reference>
              <el-button link type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination background v-model:current-page="page" v-model:page-size="pageSize" :page-sizes="pageSizeNum"
                   :total="totalNums" layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


    <el-dialog v-model="dialogShow" :title="dialogParams.type == 'add'?'添加':'编辑'" width="660px" :append-to-body="true" :destroy-on-close="true"
               :close-on-click-modal="false" @close="closeDialog">
      <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules" label-width="120px">

        <el-form-item label="开始单元数" prop="begin_unit_num" required>
          <el-input v-model.trim="dialogForm.begin_unit_num" clearable placeholder="请输入开始单元数" style="width: 30%"/>
          <el-text type="danger" style="margin-left: 5px">格式：纯数字或者纯字母</el-text>
        </el-form-item>
        <el-form-item label="结束单元数" prop="end_unit_num" required>
          <el-input v-model.trim="dialogForm.end_unit_num" clearable placeholder="请输入结束单元数" style="width: 30%"/>
          <el-text type="danger" style="margin-left: 5px">格式：纯数字或者纯字母</el-text>
        </el-form-item>
        <el-form-item label="XX栋" prop="dong_number" required>
          <el-input v-model.trim="dialogForm.dong_number" clearable placeholder="请输入XX栋"/>
        </el-form-item>
        <el-form-item label="总层数" prop="layer_num" required>
          <el-input v-model.trim="dialogForm.layer_num" clearable placeholder="请输入层数" oninput="value=value.replace(/[^\d]/g,'')" style="width: 30%" />
          <el-text type="danger" style="margin-left: 5px">格式：数字</el-text>
        </el-form-item>
        <el-form-item label="每层户数" prop="house_number" required>
          <el-input v-model.trim="dialogForm.house_number" clearable placeholder="请输入层数" oninput="value=value.replace(/[^\d]/g,'')" style="width: 30%" />
          <el-text type="danger" style="margin-left: 5px">格式：数字</el-text>
        </el-form-item>
        <el-form-item>
          <div class="flexs">
            <el-button type="primary" :loading="dialogParams.loading" @click="submitDialogForm(dialogFormRef)">
              确认
            </el-button>
            <el-button @click="resetDialogForm(dialogFormRef)">重置</el-button>
          </div>
        </el-form-item>
      </el-form>
    </el-dialog>

    <go-back/>
  </div>
</template>

<script setup>
import {
  getCurrentInstance,
  ref,
  reactive,
  onMounted,
  watch
} from "vue"
import listMixins from "@mixins/list"
import {
  useRoute,
  useRouter
} from "vue-router"
import {
  Search
} from "@element-plus/icons-vue"
import GoBack from "@components/GoBack.vue"

const {
  proxy
} = getCurrentInstance()

/**
 * minxin列表加载
 */
const {
  page,
  pageSizeNum,
  pageSize,
  loading,
  totalNums,
  searchParams,
  searchBtnLoading,
  apiPath,
  list,
  dialogFormRef,
  dialogShow,
  dialogForm,
  dialogParams,
  handleSearch,
  getData,
  handleAdd,
  handleEdit,
  resetDialogForm,
  handleAllSelected,
  submitDialogForm,
  closeDialog,
  handleAction
} = listMixins()

const router = useRouter()
const route = useRoute()
const min_area_id = route.query.min_area_id || ''
searchParams.min_area_id = min_area_id

dialogForm.begin_unit_num = '';
dialogForm.end_unit_num = '';
dialogForm.layer_num = '';
dialogForm.house_number = '';
dialogForm.dong_number = '';

apiPath.value = 'admin/minArea/unit/index';
dialogParams.addApiPath = 'admin/minArea/unit/create';
dialogParams.editApiPath = 'admin/minArea/unit/update';

onMounted(() => {
  getData()
})

const dialogFormRules = reactive({
  begin_unit_num: [{
    required: true,
    message: "请输入开始单元数",
    trigger: "blur"
  },{
    validator: proxy.$util.validNumOrLetter,
    trigger: "blur"
  }],
  end_unit_num: [{
    required: true,
    message: "请输入结束单元数",
    trigger: "blur"
  },{
    validator: proxy.$util.validNumOrLetter,
    trigger: "blur"
  }],
  layer_num: [{
    required: true,
    message: "请输入层数",
    trigger: "blur"
  }],
  house_number: [{
    required: true,
    message: "请输入每层户数",
    trigger: "blur"
  }],
  dong_number: [{
    required: true,
    message: "请输入XX栋",
    trigger: "blur"
  }],
})


const handleDelete=(row)=> {
  handleAction('admin/minArea/unit/delete', {
    min_area_unit_id: row.min_area_unit_id,
  })
}

</script>

<style lang="less">

</style>